<template>
	<view class="userInfo-content">
		<view class="info">
			<!-- 头像 -->
			<view class="avatar" style="margin:20rpx 0;">
				<u-avatar :src="data.userInfo.image.path_url|| ''" size="50"></u-avatar>
				<view style="margin-left: 40rpx;" @click="uploadAvatarImage">
					<u--text text="更改头像" type="primary"></u--text>
				</view>
			</view>
			<view>
				<u-cell-group>
					<u-cell size="large" title="昵称" :value="data.userInfo?.show_name"></u-cell>
					<u-cell size="large" title="企业名称" :value="data.userInfo?.company_name"></u-cell>
					<u-cell size="large" title="手机号" :value="data.userInfo.member?.cellphone_secret"></u-cell>
					<u-cell size="large" title="性别"
						:value="data.userInfo?.sex_str?data.userInfo?.sex_str:'保密'"></u-cell>
					<u-cell size="large" title="年龄" :value="data.userInfo?.age?data.userInfo?.age:'保密'"></u-cell>
					<u-cell size="large" title="设置登录密码" isLink url="/pages/my/enterprise/userInfo/password"></u-cell>
					<u-cell size="large" title="设置签署密码" url="/pages/my/enterprise/userInfo/signPassword"
						isLink></u-cell>
					<u-cell size="large" :title="store.state.userType == 1?'去企业':'去个人'" isLink
						@click="changeUserType"></u-cell>
					<u-cell size="large" title="切换子账号" isLink @click="changeUserList"></u-cell>
				</u-cell-group>
			</view>
		</view>
		<view class="flex button-box">
			<u-button type="primary" text="修改昵称" class="custom-style" @click="showModal"
				style="margin-right: 40rpx;"></u-button>
			<view style="width: 40rpx;"></view>
			<u-button type="error" :plain="true" text="退出登录" class="custom-style" @click="loginOut"></u-button>
		</view>
	</view>
	<!-- 修改昵称 -->
	<u-modal :show="show" title="修改昵称" :show-cancel-button="true" @confirm="confirm" @cancel="show=false">
		<view class="slot-content">
			<u--input placeholder="请输入昵称" border="surround" v-model="nickName"></u--input>
		</view>
	</u-modal>
	<user-switch ref="userSwitchListRef"></user-switch>
</template>
<script setup>
	import userSwitch from "@/components/user-switch-list.vue"
	import {
		getEnterpriseInfo,
		switchLogin,
		editEnterpriseInfo,
		userSwitchList
	} from "../../../../api/module/mine.js";
	import {
		enterpriseLoginOut
	} from "../../../../api/module/login.js";
	import {
		uploadImageBase64
	} from "../../../../api/module/common.js";
	import {
		ref,
		reactive,
		onBeforeMount,
		onMounted
	} from "vue"
	import {
		useStore
	} from 'vuex';
	import {
		goRedirect,
		goSwitchTab
	} from "../../../../utils/common";
	const userSwitchListRef = ref();
	const store = useStore()
	let info = uni.getStorageSync('userInfo')
	const data = reactive({
		userInfo: info,
		fileList6: [],
	})
	const nickName = ref('')
	const show = ref(false)
	const switchList = ref([])
	onBeforeMount(() => {
		getUserInfo()
	})
	onMounted(() => {
		userSwitchList().then(res => {
			switchList.value = res.data
		})
	})
	const changeUserList = () => {
		if (switchList.value.length == 0) {
			return;
		}
		userSwitchListRef.value.open(switchList.value);
	}
	const loginOut = () => {
		enterpriseLoginOut().then(res => {
			uni.setStorageSync("token", '')
			uni.setStorageSync("userInfo", null)
			goRedirect('/pages/alogin/index');
		})
	}
	const showModal = () => {
		show.value = true
	}
	const confirm = () => {
		if (!nickName.value) {
			uni.$u.toast('请输入手机号')
			return
		}
		editEnterpriseInfo({
			id: data.userInfo.id,
			nick_name: nickName.value
		}).then(resule => {
			getUserInfo()
			show.value = false
		})
	}
	const changeUserType = () => {
		switchLogin().then(res => {
			uni.setStorageSync("token", res.token)
			uni.setStorageSync("userInfo", res.member)
			store.commit('changeUserType', 1)
			goSwitchTab('/pages/my/index');
		});
	}
	const getUserInfo = () => {
		getEnterpriseInfo().then(res => {
			data.userInfo = res
			nickName.value = res.nick_name
		}).catch(err => {
			console.log(err)
		})
	}
	const uploadAvatarImage = () => { //选择图片  转base64
		uni.chooseImage({
			count: 1, //上传图片数量
			sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
			sourceType: ['album', 'camera'], //album 从相册选图，camera 使用相机，默认二者都有
			success: (chooseImageRes) => {
				//tempFilePaths 图片的本地文件路径列表
				const tempFilePaths = chooseImageRes.tempFilePaths[0];
				uni.getFileSystemManager().readFile({
					filePath: tempFilePaths, // 要读取的文件路径
					encoding: 'base64', // 编码格式
					success: function(res) {
						uploadAndEditImage(res.data)
					}
				})
			}
		});
	}
	const uploadAndEditImage = (url) => {
		let imageParams = {
			ext: 'png',
			content: url,
			org_name: new Date().getTime() + '.png'
		}
		uploadImageBase64(imageParams).then(res => {
			let image = []
			image.push(res.id)
			let params = {
				id: data.userInfo.id,
				image
			}
			editEnterpriseInfo(params).then(resule => {
				getUserInfo()
			})
		})
	}
</script>
<style lang="less" scoped src="/static/css/my/userInfo.scss"></style>